Const ForReading   = 1
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject ("WScript.shell")
Set objTomcat = objFSO.OpenTextFile("Config\Tomcat_config.txt", ForReading)
Set objIBoss = objFSO.OpenTextFile("Config\iBoss_config.txt", ForReading)
tomcatVersions = Array ("Tomcat 6.0","Tomcat 7.0", "Tomcat 6", "Tomcat 7", "Tomcat6", "Tomcat7", "Tomcat")

Dim LogName, myDateString , tomcatRootLocation, ibossLocation
tomcatCatAuto = true
myDateString = FormatDateTime(Date(), 1)
LogName = "Log\" + "LogFile_" & myDateString + ".txt" 
Set objLog = objFSO.OpenTextFile( LogName, ForAppending, true)

objLog.WriteLine "//============================== Start =================================//"
'Get Tomcat Path and Version
tomcatVersion = objTomcat.ReadLine
tomcatVersion = Trim(replace(replace(tomcatVersion,"tomcat_version",""),"=",""))
tomcatRootLocation = objTomcat.ReadLine
tomcatRootLocation = Trim(replace(replace(tomcatRootLocation,"tomcat_path",""),"=",""))
tomcatAuto = objTomcat.ReadLine
tomcatAuto = Trim(replace(replace(tomcatAuto,"tomcat_auto_restart",""),"=",""))
WriteLog "Found Tomcat Location: "+tomcatRootLocation
WriteLog "Found Tomcat Verion: "+tomcatVersion
WriteLog "Tomcat Auto Run Started"

Comp = StrComp(tomcatAuto, "on", vbTextCompare)
If Comp = 0 Then
	tomcatCatAuto = true
Else 
	tomcatCatAuto = false
End If

Function WriteLog(log)
	objLog.WriteLine ("["&Time&"]: ") + log
End Function

'Tomcat Control Funtion,  Action = start or stop
Function tomcatControl(Action)
	Set tomcatControl = CreateObject("System.Collections.ArrayList")
	For Each tomcatVersion in tomcatVersions
		commandKey = "cmd /K net "+Action+ " "+ tomcatVersion + "& exit"
		TomcatOutput = objShell.run(commandKey, 0, true)
	Next
	compAndPop Action, "stop", "Tomcat Stopped", "Tomcat", 1
	compAndPop Action, "start", "Tomcat Started", "Tomcat", 1
End Function

Function compAndPop(str1, str2, txt, title, sec)
	dim bool
	bool = StringEquals ( str1, str2)
	IF bool Then
		objShell.Popup txt, sec , title, 4096
		WriteLog " tomcat " + str1 + " success"
	End If
End Function

Function StringEquals(str1, str2)
	Comp = StrComp(str1, str2)
	If Comp = 0 Then
		StringEquals = true
	Else 
		StringEquals = false
	End If
End Function

'Stop Tomcat 
tomcatControl "stop"

If objFSO.FolderExists(tomcatRootLocation) Then
	If objFSO.FolderExists(tomcatRootLocation + "\webapps\iBoss2JWS") Then 
		WriteLog "Removing JWS Folder in Tomcat"
		Set origionalJwsFolderObj = objFSO.GetFolder(tomcatRootLocation + "\webapps\iBoss2JWS")
		origionalJwsFolderObj.Delete True
		WriteLog "Remove Complete"
	Else
		WriteLog "JWS Folder Not Exist in Tomcat Folder"
	End If
	If objFSO.FolderExists(tomcatRootLocation + "\webapps\iBoss2RS") Then 
		WriteLog "Removing RS Folder in Tomcat"
		Set origionalRSFolderObj = objFSO.GetFolder(tomcatRootLocation + "\webapps\iBoss2RS")
		origionalRSFolderObj.Delete True
		WriteLog "Remove Complete"
	Else
		WriteLog "RS Folder Not Exist in Tomcat Folder"
	End If
	If objFSO.FileExists(tomcatRootLocation + "\webapps\iBoss2JWS.war") Then 
		WriteLog "Removing iBoss2JWS.war in Tomcat"
		Set origionalJwsWarObj = objFSO.GetFile(tomcatRootLocation + "\webapps\iBoss2JWS.war")
		origionalJwsWarObj.Delete
		WriteLog "Remove Complete"
	Else
		WriteLog "iBoss2JWS.war Not Exist in Tomcat Folder"
	End If
	If objFSO.FileExists(tomcatRootLocation + "\webapps\iBoss2RS.war") Then 
		WriteLog "Removing iBoss2RS.war in Tomcat"
		Set origionalRSWarObj = objFSO.GetFile(tomcatRootLocation + "\webapps\iBoss2RS.war")
		origionalRSWarObj.Delete
		WriteLog "Remove Complete"
	Else
		WriteLog "iBoss2RS.war Not Exist in Tomcat Folder"
	End If
	
	'get iboss2 Path and Version
	ibossVersion = objIBoss.ReadLine
	ibossVersion = Trim(replace(replace(ibossVersion,"iboss_version",""),"=",""))
	ibossLocation = objIBoss.ReadLine
	ibossLocation = Trim(replace(replace(ibossLocation,"iboss_path",""),"=",""))
	
	WriteLog "Found iBoss2 Location: " + ibossLocation
	If objFSO.FolderExists(ibossLocation+"\iBoss2JWS") Then
		If objFSO.FolderExists(ibossLocation+"\iBoss2JWS\target") Then 
			If objFSO.FileExists(ibossLocation+"\iBoss2JWS\target\iBoss2JWS.war") Then
				WriteLog "Found iBoss2JWS.war, Copying to Tomcat Folder"
				Set objJwsFile = objFSO.GetFile(ibossLocation+"\iBoss2JWS\target\iBoss2JWS.war")
				objJwsFile.Copy tomcatRootLocation + "\webapps\"
				WriteLog "iBoss2JWS.war copy completed"
			Else
				Wscript.Echo "iBoss2JWS Folder Missing iBoss2JWS.war, Please recompile again"
			End IF
		Else
			Wscript.Echo "iBoss2JWS Folder Missing Target Folder, Please recompile again"
		End If
	Else
		Wscript.Echo "iBoss2JWS Folder Not Exist in iBoss Folder"
	End If
	
	If objFSO.FolderExists(ibossLocation+"\iBoss2RS") Then
		If objFSO.FolderExists(ibossLocation+"\iBoss2RS\target") Then 
			If objFSO.FileExists(ibossLocation+"\iBoss2RS\target\iBoss2RS.war") Then
				WriteLog "Found iBoss2RS.war, Copying to Tomcat Folder"
				Set objRSFile = objFSO.GetFile(ibossLocation+"\iBoss2RS\target\iBoss2RS.war")
				objRSFile.Copy tomcatRootLocation + "\webapps\"
				WriteLog "iBoss2RS.war copy completed"
			Else
				Wscript.Echo "iBoss2RS Folder Missing iBoss2RS.war, Please recompile again"
			End IF
		Else
			Wscript.Echo "iBoss2RS Folder Missing Target Folder, Please recompile again"
		End If
	Else
		Wscript.Echo "iBoss2JWS Folder Not Exist in iBoss Folder"
	End If
Else
	Wscript.Echo "Tomcat Location Error"
End If


'Start Tomcat 
tomcatControl "start"

objLog.WriteLine "//=============================== End ==================================//"
objShell.Popup "Deploy Completed!", 1, "Deploy Status"
objTomcat.Close
objIBoss.Close